<!--   
  个人信息设置
 -->
<template>
	<view class="set-mine">
		<cu-custom :isBack="true" bgColor="bg-fa">
			<block slot="content">个人信息设置</block>
		</cu-custom>
		<view class="mine-box">
			<view class="mine-item">
				<view class="mine-label">
					我的头像
				</view>
				<view class="centent-box head-box" @tap="chooseImage()">
					<image class="head" v-if="head.length>0" :src="head"></image>
				</view>
				<view class="mine-right" @tap="chooseImage()">
					<text class="iconfont iconxiangyou1"></text>
				</view>
			</view>
			
			<view class="mine-item">
				<view class="mine-label">
					昵称
				</view>
				<view class="centent-box" @tap="nikeShow = true">
					{{nickName}}
				</view>
				<view class="mine-right"  @tap="nikeShow = true">
					<text class="iconfont iconxiangyou1"></text>
				</view>
			</view>
			
			<view class="mine-item">
				<view class="mine-label">
					性别
				</view>
				<view class="centent-box"  @tap="sexShow = true">
					{{sex == "M" ? '男':'女'}}
				</view>
				<view class="mine-right"  @tap="sexShow = true">
					<text class="iconfont iconxiangyou1"></text>
				</view>
			</view>
			
			<view class="mine-item">
				<view class="mine-label">
					生日
				</view>
				<view class="centent-box" @tap="timeShow = true">
					{{birthday}}
				</view>
				<view class="mine-right"  @tap="timeShow = true">
					<text class="iconfont iconxiangyou1"></text>
				</view>
			</view>
		</view>
		
		
		<view class="submit-btn">
			<u-button @tap="submitInfo" shape="circle" :ripple="true" :custom-style="{'background':'#FA436A','borderColor':'#FA436A','color':'#fff'}">保存</u-button>
		</view>
		
		<!--  修改昵称弹窗  -->
		<u-mask :show="nikeShow" @tap="nikeShow = false">
			<view class="warp">
				<view class="rect" @tap.stop>
					<view class="alert-title">
						修改名称
					</view>
					<view class="alert-text">
						<input type="text" :maxlength="12" placeholder="请输入昵称" class="inp" v-model="nike" />
					</view>
					<view class="alert-btn">
						<view class="btn-con" @tap.stop="nikeShow = false">取消</view>
						<view class="btn-con" @tap.stop="cancelNike">确认</view>
					</view>
				</view>
			</view>
		</u-mask>
		<!-- 修改昵称弹窗end -->
		
		<!--  选择生日  -->
		<u-picker @confirm="timeConfirm" v-model="timeShow" mode="time" :default-time="birthday" :start-year="1950" :end-year="endYear" :params="timeParams"></u-picker>
		<!-- 选择生日 -->
		
		<!--  选择性别弹窗  -->
		<u-select v-model="sexShow" mode="single-column" :list="sexList" @confirm="sexConfirm"></u-select>
		<!--  选择性别弹窗  -->
		
		<w-compress ref='wCompress' />
	</view>
</template>

<script>
	import WCompress from '@/components/w-compress/compress.js'
	import {mapState} from "vuex"
	import {hasUserInfo} from "@/utils/hasLogin.js"
	export default {
		name:"setMine",
		data(){
			return {
				head:"",
				birthday:"",
				sex:"",
				nickName:"",
				/*  显示修改昵称弹窗  */
				nikeShow:false,
				nike:"",
				
				/*  选择性别弹窗  */
				sexList:[
					{label:"男",value:"M"},
					{label:"女",value:"W"}
				],
				sexShow:false,
				
				/*  显示生日弹窗  */
				timeShow:false,
				timeParams: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				endYear:(new Date).getFullYear(),
			}
		},
		computed:{
			...mapState("user",['userInfo'])
		},
		methods:{
			
			/*  提交信息  */
			submitInfo(){
				let data = {
					photo:this.head,
					birthDate:this.birthday,
					sex:this.sex,
					nickName:this.nickName
				}
				data = Object.assign({},this.userInfo,data)
				this.$ajax("updateUserInfo",data).then((res)=>{
					if(res.code>=0){
						uni.showToast({
							title:"修改成功",
							icon:"success",
							duration:1500
						})
						let t = setTimeout(()=>{
							clearTimeout(t)
							uni.switchTab({
								url:"/pages/index/nime"
							})
						},1500)
					}
				})
			},
			
			/*  修改性别  */
			sexConfirm(e){
				this.sex = e[0].value
			},
			
			/*   修改生日  */
			timeConfirm(e){
				this.birthday = e.year + '-' + e.month + '-' + e.day
			},
			
			/*  修改昵称点击弹窗确认  */
			cancelNike(){
				if(this.nike.trim().length == 0){
					uni.showToast({
						duration:1500,
						icon:"none",
						title:"请输入昵称"
					})
					return false
				}
				if(this.nike.trim().length > 12){
					uni.showToast({
						duration:1500,
						icon:"none",
						title:"昵称最长是12位"
					})
					return false
				}
				this.nickName = this.nike
				this.nikeShow = false
			},
			
			
			/*  图片上传  */
			async chooseImage(){
				let path = null
				await this.$chooseImage().then((res)=>{
					path = res
				})
				if(path == null){
					return false
				}
				let compressUrl = null
				await this.$refs.wCompress.start(path,{
					type:"jpg",
					quality: 0.8,     // 压缩质量，默认0.8
					base64: true,     // 是否返回base64，默认false，非H5有效
				}).then((url)=>{
					compressUrl = url
				}).catch((err)=>{
					uni.showToast({
						icon:"none",
						duration:2000,
						mask:true,
						title:"请重新上传图片"
					})
				})
				
				if(compressUrl == null){
					return false
				}
				
				this.$ajaxFile(compressUrl).then((res)=>{
					if(res.code>=0){
						this.head = res.data
					}
				})
			}
		},
		onLoad() {
			let has = hasUserInfo()
			if(has){
				this.head = this.userInfo.photo
				this.nickName = this.userInfo.nickName
				this.sex = this.userInfo.sex
				this.birthday = this.userInfo.birthDate
			}
		}
	}
</script>

<style lang="scss" scoped>
	.set-mine{
		width: 100%;
		.mine-box{
			width: 100%;
			background: #FFFFFF;
			padding: 0 20rpx;
			line-height: 1.3;
			.mine-item{
				padding: 20rpx 0;
				display: flex;
				justify-content: space-between;
				align-items: center;
				.mine-label{
					font-size: 32rpx;
					color: #333333;
				}
				.centent-box{
					flex-grow: 1;
					text-align: right;
					font-size: 28rpx;
					color: #999999;
				}
				.head-box{
					height: 80rpx;
				}
				.head{
					display: inline-block;
					width: 80rpx;
					height: 80rpx;
					border-radius: 80rpx 80rpx;
				}
				.mine-right{
					.iconfont{
						color: #8a8a8a;
						font-size: 36rpx;
					}
					
				}
			}
		}
		.submit-btn{
			position: fixed;
			left: 0;
			right: 0;
			width: 100%;
			padding: 0 20rpx;
			bottom: 40rpx;
		}
		
		
		/*  修改昵称弹窗  */
		.warp {
			display: flex;
			align-items: center;
			justify-content: center;
			height: 100%;
			width: 100%;
		}
		.rect {
			width: 80%;
			height: 320rpx;
			background-color: #fff;
			.alert-title{
				width: 100%;
				height: 100rpx;
				padding-top: 20rpx;
				padding-bottom: 20rpx;
				line-height:60rpx;
				font-size: 32rpx;
				color: #333333;
				text-align: center;
				font-weight: bold;
				box-sizing: border-box;
			}
			.alert-text{
				padding: 0 40rpx;
				height: 140rpx;
				color: #333333;
				font-size: 26rpx;
			}
			.alert-btn{
				display: flex;
				justify-content: flex-end;
				align-items: flex-start;
				padding: 0 20rpx;
				.btn-con{
					color: #60bdb5;
					font-size: 26rpx;
					padding: 0 20rpx;
					margin-left: 20rpx;
				}
			}
			.inp{
				height: 80rpx;
				border-bottom: 4rpx solid #c1c1c1;
			}
		}
		/*  修改昵称弹窗  */
		
	}
</style>
